/**
 * 文件上传管理器
 */
class FileUploadManager {
    constructor(options) {
        this.chatInput = options.chatInput;
        this.onFileSelect = options.onFileSelect;
        this.maxFileSize = options.maxFileSize || 10 * 1024 * 1024; // 默认10MB
        this.allowedTypes = options.allowedTypes || [
            'image/jpeg', 
            'image/png',
            'image/gif',
            'video/mp4',
            'audio/mpeg',
            'application/pdf'
        ];
        
        this.createFileInput();
    }
    
    /**
     * 创建隐藏的文件输入元素
     */
    createFileInput() {
        this.fileInput = document.createElement('input');
        this.fileInput.type = 'file';
        this.fileInput.style.display = 'none';
        this.fileInput.multiple = true;
        this.fileInput.accept = this.allowedTypes.join(',');
        
        this.fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                this.handleFiles(e.target.files);
            }
        });
        
        document.body.appendChild(this.fileInput);
    }
    
    /**
     * 处理选择的文件
     * @param {FileList} files 
     */
    handleFiles(files) {
        const validFiles = [];
        
        for (let i = 0; i < files.length; i++) {
            const file = files[i];
            
            // 验证文件类型
            if (!this.allowedTypes.includes(file.type)) {
                console.warn(`不支持的文件类型: ${file.type}`);
                continue;
            }
            
            // 验证文件大小
            if (file.size > this.maxFileSize) {
                console.warn(`文件大小超过限制: ${file.name}`);
                continue;
            }
            
            validFiles.push(file);
        }
        
        if (validFiles.length > 0) {
            this.onFileSelect(validFiles);
        }
        
        // 重置文件输入，允许重复选择同一文件
        this.fileInput.value = '';
    }
    
    /**
     * 触发文件选择
     */
    triggerFileSelect() {
        this.fileInput.click();
    }
    
    /**
     * 销毁文件上传管理器
     */
    destroy() {
        if (this.fileInput && this.fileInput.parentNode) {
            this.fileInput.parentNode.removeChild(this.fileInput);
        }
    }
}

/**
 * 创建文件预览元素
 * @param {File} file 
 * @returns {HTMLElement}
 */
function createFilePreview(file) {
    const container = document.createElement('div');
    container.className = 'file-preview';
    
    // 根据文件类型创建不同的预览
    if (file.type.startsWith('image/')) {
        const img = document.createElement('img');
        img.src = URL.createObjectURL(file);
        container.appendChild(img);
    } 
    else if (file.type.startsWith('video/')) {
        const video = document.createElement('video');
        video.controls = true;
        video.src = URL.createObjectURL(file);
        container.appendChild(video);
    }
    else if (file.type.startsWith('audio/')) {
        const audio = document.createElement('audio');
        audio.controls = true;
        audio.src = URL.createObjectURL(file);
        container.appendChild(audio);
    }
    else {
        const icon = document.createElement('div');
        icon.className = 'file-icon';
        
        const name = document.createElement('span');
        name.textContent = file.name;
        
        container.appendChild(icon);
        container.appendChild(name);
    }
    
    return container;
}

/**
 * 创建上传进度条
 * @param {number} total 
 * @returns {HTMLElement}
 */
function createProgressBar(total) {
    const progressContainer = document.createElement('div');
    progressContainer.className = 'upload-progress';
    
    const progressBar = document.createElement('div');
    progressBar.className = 'progress-bar';
    
    const progressText = document.createElement('span');
    progressText.className = 'progress-text';
    progressText.textContent = '0%';
    
    progressContainer.appendChild(progressBar);
    progressContainer.appendChild(progressText);
    
    return {
        container: progressContainer,
        update: (loaded) => {
            const percent = Math.round((loaded / total) * 100);
            progressBar.style.width = `${percent}%`;
            progressText.textContent = `${percent}%`;
            
            if (percent >= 100) {
                setTimeout(() => {
                    progressContainer.remove();
                }, 1000);
            }
        }
    };
}